// pages/taxCalculation/taxCalculation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
		/* 工资薪金所得 */
		wages:"",
		/* 劳务报酬所得 */
		serviceCharge:"",
		/* 稿酬所得 */
		remuneration:"",
		/* 特许权使用费用所得 */
		usePay:"",
		/* 子女教育 */
		childPay:"",
		/* 继续教育 */
		continuePay:"",
		/* 大病医疗 */
		illPay:"",
		/* 住房租金 */
		housePay:"",
		/* 房贷利息 */
		housingLoan:"",
		/* 赡养老人 */
		supportElder:"",
		/* 免税收入 */
		dutyFree:"",
		/* 三险一金 */
		insurance:"",
		/* 其他扣除 */
		otherDeduct:"",
		/* 扣除费用 */
		otherPay:"",
		/* 转存参数 */
		intermediateParameter:"",
		/* 已预缴个人所得税 */
		personalTax:"",
		/* 所得税类型 */
		preList:["汇算清缴","预扣预缴","年终奖","利息、股息、红利所得","财产租赁所得","财产转让所得","偶然所得"],
		nextList:["综合所得","经营所得"],
		/* 月 */
		monthList:["1","2","3","4","5","6","7","8","9","10","11","12"],
		/* 是否出租 */
		leaseList:["是","否"],
		preIndex:0,
		nextIndex:0,
		monthIndex:0,
		leaseIndex:0,
		/* 初始化收入高亮 */
		style:1,
		/* 计算结果 */
		result:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		 wx.setNavigationBarTitle({ title: '个税计算' });
		 wx.showShareMenu({  
			withShareTicket:true,
			menus: ['shareAppMessage', 'shareTimeline']
		 })
  },
	/* 计算类型 */
	preChange:function(e){
		
		if(e.detail.value==0){
			this.setData({
				nextList:["综合所得","经营所得"],
				nextIndex:0
			})
		}else if(e.detail.value==1){
			this.setData({
				nextList:["工资、薪金所得","劳务报酬所得","稿酬所得","特许权使用费所得"],
				nextIndex:0
			})
		}
		var _this=this;
		var timer=setTimeout(function(){
			_this.setData({
				preIndex: e.detail.value,
				result:"",
				wages:"",
				serviceCharge:"",
				remuneration:"",
				usePay:"",
				childPay:"",
				continuePay:"",
				illPay:"",
				housePay:"",
				housingLoan:"",
				supportElder:"",
				dutyFree:"",
				insurance:"",
				otherDeduct:"",
				otherPay:"",
				personalTax:""
			})
		},50);
	},
	nextChange:function(e){
		var _this=this;
		var timer=setTimeout(function(){
			_this.setData({
				nextIndex: e.detail.value,
				result:"",
				wages:"",
				serviceCharge:"",
				remuneration:"",
				usePay:"",
				childPay:"",
				continuePay:"",
				illPay:"",
				housePay:"",
				housingLoan:"",
				supportElder:"",
				dutyFree:"",
				insurance:"",
				otherDeduct:"",
				otherPay:"",
				personalTax:""
			})
		},50);
	}, 
	/* 是否为出租 */
	leaseChange:function(e){
		this.setData({
			leaseIndex: e.detail.value,
			result:"",
			wages:"",
			serviceCharge:"",
			remuneration:"",
			usePay:"",
			childPay:"",
			continuePay:"",
			illPay:"",
			housePay:"",
			housingLoan:"",
			supportElder:"",
			dutyFree:"",
			insurance:"",
			otherDeduct:"",
			otherPay:"",
			personalTax:""
		})
	},
	/* 选择月份 */
	monthChange:function(e){
		this.setData({
			monthIndex: e.detail.value,
		})
	},
	/* 高亮收入时期 */
	income:function(e){
		var _this=this;
		var timer=setTimeout(function(){
			_this.setData({
				style:e.currentTarget.dataset.index,
				result:"",
				wages:"",
				serviceCharge:"",
				remuneration:"",
				usePay:"",
				childPay:"",
				continuePay:"",
				illPay:"",
				housePay:"",
				housingLoan:"",
				supportElder:"",
				dutyFree:"",
				insurance:"",
				otherDeduct:"",
				otherPay:"",
				personalTax:""
			})
		},50);
	},
	/* 输入框聚焦和失去焦点公共方法 */
	commonFn:function(sort,type){
		let _this=this;
		let values="";
		//if(type=="blur"){
			values=_this.data.intermediateParameter;
		/* }else{
			values="";
		} */
		if(sort==1){
			this.setData({
				wages: values,
			})
		}else if(sort==2){
			this.setData({
				serviceCharge: values,
			})
		}else if(sort==3){
			this.setData({
				remuneration: values,
			})
		}else if(sort==4){
			this.setData({
				usePay: values,
			})
		}else if(sort==5){
			this.setData({
				dutyFree: values,
			})
		}else if(sort==6){
			this.setData({
				insurance: values,
			})
		}else if(sort==7){
			this.setData({
				childPay: values,
			})
		}else if(sort==8){
			this.setData({
				continuePay: values,
			})
		}else if(sort==9){
			this.setData({
				illPay: values,
			})
		}else if(sort==10){
			this.setData({
				housePay: values,
			})
		}else if(sort==11){
			this.setData({
				housingLoan: values,
			})
		}else if(sort==12){
			this.setData({
				supportElder: values,
			})
		}else if(sort==13){
			this.setData({
				otherDeduct:values,
			})
		}else if(sort==14){
			this.setData({
				otherPay: values,
			})
		}else if(sort==15){
			this.setData({
				personalTax: values,
			})
		}
		this.setData({
			intermediateParameter:""
		})
	},
	/* 输入框聚焦 */
	/* focusInput:function(e){
		let sort=e.currentTarget.dataset.sort;
		this.commonFn(sort,'focus');
	}, */
	/* 输入框失去焦点 */
	/* blurs:function(e){
		let sort=e.currentTarget.dataset.sort;
		this.commonFn(sort,'blur');
	}, */
	/* 获取输入框的值 */
	obtainValue:function(e){
		if(!e.detail.value){
			e.detail.value="";
		}else{
			if(e.detail.value>10000000000){
				wx.showToast({
					icon:"none",
					title:"输入值最大为百亿"
				})
				if(e.detail.value.indexOf(".")>-1){
					e.detail.value=e.detail.value.substring(0,e.detail.value.length-2);
				}else{
					e.detail.value=e.detail.value.substring(0,e.detail.value.length-1);
				}
			}else{
				e.detail.value=e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
			}
		}
		this.setData({
			intermediateParameter:e.detail.value
		})
		let sort=e.currentTarget.dataset.sort;
		this.commonFn(sort,'focus');
	},
	/* 计算结果 */
	calculation:function(){
		this.data.result=this.data.result?this.data.result:"0";
		this.data.wages=this.data.wages?this.data.wages:"0";
		this.data.serviceCharge=this.data.serviceCharge?this.data.serviceCharge:"0";
		this.data.remuneration=this.data.remuneration?this.data.remuneration:"0";
		this.data.usePay=this.data.usePay?this.data.usePay:"0";
		this.data.childPay=this.data.childPay?this.data.childPay:"0";
		this.data.continuePay=this.data.continuePay?this.data.continuePay:"0";
		this.data.illPay=this.data.illPay?this.data.illPay:"0";
		this.data.housePay=this.data.housePay?this.data.housePay:"0";
		this.data.housingLoan=this.data.housingLoan?this.data.housingLoan:"0";
		this.data.supportElder=this.data.supportElder?this.data.supportElder:"0";
		this.data.dutyFree=this.data.dutyFree?this.data.dutyFree:"0";
		this.data.insurance=this.data.insurance?this.data.insurance:"0";
		this.data.otherDeduct=this.data.otherDeduct?this.data.otherDeduct:"0";
		this.data.otherPay=this.data.otherPay?this.data.otherPay:"0";
		this.data.personalTax=this.data.personalTax?this.data.personalTax:"0";
		var _this=this;
		var timer=setTimeout(function(){
			/* 综合所得  */
			if(_this.data.preIndex==0&&_this.data.nextIndex==0){
				_this.data.otherPay=60000;
			}
			/* 工资薪金所得 */
			if(_this.data.preIndex==1&&_this.data.nextIndex==0){
				_this.data.otherPay=(parseInt(_this.data.monthIndex)+1)*5000;
			}
			/* 劳务报酬 */
			if(_this.data.preIndex==1&&_this.data.nextIndex==1){
				_this.data.otherPay=_this.data.wages>4000?_this.data.wages*0.2:800;
			}
			
			/* 稿酬 */
			if(_this.data.preIndex==1&&_this.data.nextIndex==2){
				_this.data.otherPay=_this.data.wages>4000?_this.data.wages*0.2:800;
				_this.data.dutyFree=(_this.data.wages-_this.data.otherPay)*0.3
			}
			/* 特许权使用费 */
			if(_this.data.preIndex==1&&_this.data.nextIndex==3){
				_this.data.otherPay=_this.data.wages>4000?_this.data.wages*0.2:800;
			}
			/* 财产租赁所得 */
			if(_this.data.preIndex==4){
				_this.data.dutyFree=_this.data.wages>4000?_this.data.wages*0.2:800;
			}
			var allIncome=parseFloat(_this.data.wages)+parseFloat(_this.data.serviceCharge)+parseFloat(_this.data.remuneration)+parseFloat(_this.data.usePay);
			var allPays=parseFloat(_this.data.childPay)+parseFloat(_this.data.continuePay)+parseFloat(_this.data.illPay)+parseFloat(_this.data.housePay)+parseFloat(_this.data.housingLoan)+parseFloat(_this.data.supportElder)+parseFloat(_this.data.dutyFree)+parseFloat(_this.data.insurance)+parseFloat(_this.data.otherDeduct)+parseFloat(_this.data.otherPay);
			var taxPay=allIncome-allPays;
			var result="0";
			if(taxPay<0){
				wx.showToast({
					icon:"none",
					title:"支出费用大于收入"
				})
			}
			/* 综合所得 工资、薪金所得 */
			if((_this.data.preIndex==0&&_this.data.nextIndex==0)||(_this.data.preIndex==1&&_this.data.nextIndex==0)){
				if(_this.data.style==1){
					if(taxPay<=36000&&taxPay>0){
						result=taxPay*0.03;
					}else if(taxPay>36000&&taxPay<=144000){
						result=taxPay*0.1-2520;
					}else if(taxPay>144000&&taxPay<=300000){
						result=taxPay*0.2-16920;
					}else if(taxPay>300000&&taxPay<=420000){
						result=taxPay*0.25-31920;
					}else if(taxPay>420000&&taxPay<=660000){
						result=taxPay*0.3-52920;
					}else if(taxPay>660000&&taxPay<=960000){
						result=taxPay*0.35-85920;
					}else if(taxPay>960000){
						result=taxPay*0.45-181920;
					}
				}else if(_this.data.style==2){
					if(taxPay<=34920&&taxPay>0){
						result=(taxPay/0.97)*0.03;
					}else if(taxPay>34920&&taxPay<=132120){
						result=((taxPay-2520)/0.9)*0.1-2520;
					}else if(taxPay>132120&&taxPay<=256920){
						result=((taxPay-16920)/0.8)*0.2-16920;
					}else if(taxPay>256920&&taxPay<=346920){
						result=((taxPay-31920)/0.75)*0.25-31920;
					}else if(taxPay>346920&&taxPay<=514920){
						result=((taxPay-52920)/0.7)*0.3-52920;
					}else if(taxPay>514920&&taxPay<=709920){
						result=((taxPay-85920)/0.65)*0.35-85920;
					}else if(taxPay>709920){
						result=((taxPay-181920)/0.55)*0.45-181920;
					}
				}
			}
			/* 经营所得 */
			if(_this.data.preIndex==0&&_this.data.nextIndex==1){
				if(taxPay<=30000&&taxPay>0){
					result=taxPay*0.05;
				}else if(taxPay>30000&&taxPay<=90000){
					result=taxPay*0.1-1500;
				}else if(taxPay>90000&&taxPay<=300000){
					result=taxPay*0.2-10500;
				}else if(taxPay>300000&&taxPay<=500000){
					result=taxPay*0.30-40500;
				}else if(taxPay>500000){
					result=taxPay*0.35-65500;
				}
			}
			/* 劳务报酬 */
			if(_this.data.preIndex==1&&_this.data.nextIndex==1){
				if(taxPay<=20000&&taxPay>0){
					result=taxPay*0.2;
				}else if(taxPay>20000&&taxPay<=50000){
					result=taxPay*0.3-2000;
				}else if(taxPay>50000){
					result=taxPay*0.4-7000;
				}
			}
			/*  稿酬 特许权使用费  */
			if((_this.data.preIndex==1&&_this.data.nextIndex==2)||(_this.data.preIndex==1&&_this.data.nextIndex==3)){
				if(taxPay>0){
					result=taxPay*0.2;
				}
			}
			/* 年终奖 */
			if(_this.data.preIndex==2){
				if(taxPay<=36000&&taxPay>0){
					result=taxPay*0.03;
				}else if(taxPay>36000&&taxPay<=144000){
					result=taxPay*0.1-2520/12;
				}else if(taxPay>144000&&taxPay<=300000){
					result=taxPay*0.2-16920/12;
				}else if(taxPay>300000&&taxPay<=420000){
					result=taxPay*0.25-31920/12;
				}else if(taxPay>420000&&taxPay<=660000){
					result=taxPay*0.3-52920/12;
				}else if(taxPay>660000&&taxPay<=960000){
					result=taxPay*0.35-85920/12;
				}else if(taxPay>960000){
					result=taxPay*0.45-181920/12;
				}
			}
			/* 股息、红利 财产转让所得 偶然所得*/
			if(_this.data.preIndex==3||_this.data.preIndex==5||_this.data.preIndex==6){
				if(taxPay>0){
					result=taxPay*0.2;
				}
			}
			/* 财产租赁所得 */
			if(_this.data.preIndex==4&&taxPay>0){
				if(_this.data.leaseIndex=="0"){
					result=taxPay*0.1;
				}else{
					result=taxPay*0.2;
				}
			}
			result=result-parseFloat(_this.data.personalTax);
			result=result.toFixed(2);
			_this.setData({
				result:result
			})
			clearTimeout(timer);
		},200)
	},
  /**
  	 * 用户点击右上角分享朋友
  	 */
  	onShareAppMessage: function (e) {
  		
  	},
  	/**
  	* 用户点击右上角分享朋友圈
  	*/
  	onShareTimeline: function () {
  
  	 },
})